package com.shenfc.fxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shenfc.fxzj.domain.Blog;
import com.shenfc.fxzj.domain.PageBean;
import com.shenfc.fxzj.mapper.BlogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shenfc.fxzj.service.BlogService;
import com.shenfc.fxzj.util.ArrayUtils;
import com.shenfc.fxzj.util.DateUtils;

import java.util.List;
import java.util.Map;

/**
 * 博客Service实现类
 *
 * @author sfc
 */
@Service("blogService")
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public PageInfo<Blog> queryBlogPage(Map<String, Object> map, PageBean pageBean) {
        PageHelper.startPage(pageBean.getPage(), pageBean.getLimit());
        List<Blog> list = blogMapper.queryBlogList(map);
        if (ArrayUtils.isNotEmpty(list)) {
            for (Blog blog : list) {
                blog.setCreateTimeStr(DateUtils.dateToString(DateUtils.DATETIME_FORMAT, blog.getCreateTime()));
            }
        }
        PageInfo<Blog> pageInfo = new PageInfo<Blog>(list);
        pageBean.setRecTotal(pageInfo.getTotal());
        return pageInfo;
    }

    @Override
    public Blog getBlogById(Long id) {
        Blog blog = blogMapper.selectByPrimaryKey(id);
        if (blog != null) {
            blog.setCreateTimeStr(DateUtils.dateToString(DateUtils.DATETIME1_FORMAT, blog.getCreateTime()));
        }
        return blog;
    }

    @Override
    public Integer update(Blog blog) {
        return blogMapper.updateByPrimaryKeySelective(blog);
    }

    @Override
    public Integer addBlog(Blog blog) {
        return blogMapper.insertSelective(blog);
    }

    @Override
    public Integer deleteBlogBatch(Long[] id) {
        return blogMapper.deleteBlogBatch(id);
    }


}
